package com.guojiganzhe.subject.common.entity;

import lombok.Data;

import java.io.Serializable;
import java.util.Collections;
import java.util.List;

/**
 * 分页返回实体
 * @author: guojiganzhe
 * @create: 2020-07-07 09:05
 **/
@Data
public class PageResult< T> implements Serializable {
    private Integer pageNo =1;//当前页码

    private Integer pageSize = 20;//每页记录数

    private Integer total = 0;//总记录数

    private Integer  totalPage = 0;//总页数

    private List<T> result = Collections.emptyList();//空列表

    private Integer start = 1;//起始索引

    private Integer end = 0;//结束索引

    public void setRecords(List<T> result ){
        this.result = result;
        if (result != null && result.size() > 0){
            setTotal(result.size());
        }
    }

    public void setTotal(Integer total){
        this.total = total;
        if (this.pageSize> 0){
           this.totalPage = (total/this.pageSize) + (total%this.pageSize==0?0:1);
        }else {
            this.totalPage = 0;
        }
        this.start = (this.pageSize > 0  ? (this.pageNo - 1) * this.pageSize : 0)+ 1 ;//起始索引
        this.end = (this.start -1 + this.pageSize *(this.pageNo >0 ? 1 : 0));//结束索引
    }

    public void setPageSize(Integer pageSize){
        this.pageSize = pageSize;
    }
    public void setPageNo(Integer pageNo){
        this.pageNo = pageNo;
    }

}
